﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Unity;
using Silverlight.Weblog.Client.DAL.Events;
using Silverlight.Weblog.Client.DAL.Services.Interfaces;
using Silverlight.Weblog.Common.IoC;

namespace Silverlight.Weblog.Client.DAL.Services
{
    [Export(typeof(IInitializationService))]
    public class NavigationFragmentListeningService : IInitializationService
    {
        public NavigationFragmentListeningService()
        {
            IoC.BuildUp(this);
        }

        [Dependency]
        public IAnalyzeNavigationFragment AnalyzeNavigationFragment { get; set; }

        [Dependency]
        public ISearchBlogPostService SearchBlogPostService { get; set; }

        [Dependency]
        public IGoToHomePageService GoToHomePageService { get; set; }

        [Dependency]
        public IGoToBlogPostService GoToBlogPostService { get; set; }

        [Dependency]
        public ICategoryBlogPostService CategoryBlogPostService { get; set; }

        public void Initialize()
        {
            Application.Current.Host.NavigationStateChanged += new EventHandler<System.Windows.Interop.NavigationStateChangedEventArgs>(Host_NavigationStateChanged);
        }

        void Host_NavigationStateChanged(object sender, System.Windows.Interop.NavigationStateChangedEventArgs e)
        {
            string fragment = e.NewNavigationState;
            var fragmentQueryString = AnalyzeNavigationFragment.GetFragmentQueryString(fragment); 

            if (AnalyzeNavigationFragment.IsHomePage(fragmentQueryString))
            {
                GoToHomePageService.NavigateToHomePage();
            }
            else if (AnalyzeNavigationFragment.IsSearchPage(fragmentQueryString))
            {
                SearchBlogPostService.SearchTerm(new SearchEventData(fragmentQueryString["Search"], fragmentQueryString["Username"]));
            }
            else if (AnalyzeNavigationFragment.IsCategoryPage(fragmentQueryString))
            {
                CategoryBlogPostService.DisplayCategory(fragmentQueryString["Category"], fragmentQueryString["Username"]);
            }
            else if (AnalyzeNavigationFragment.IsBlogPostPage(fragmentQueryString))
            {
                GoToBlogPostService.NavigateToBlogPostPermlinkEvent(fragmentQueryString["BlogPost"]);
            }
        }

        public void StartService(ApplicationServiceContext context)
        {
        }

        public void StopService()
        {
            Application.Current.Host.NavigationStateChanged -= new EventHandler<System.Windows.Interop.NavigationStateChangedEventArgs>(Host_NavigationStateChanged);
        }

    }
}



